package com.fenghongzhang.day010_test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private Button select;
    private ContentResolver contentResolver;
    private Uri uri;
    private Button del;
    private Button insert;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //内容解析者
         contentResolver = getContentResolver();
         uri = Uri.parse("content://com.feng");


        insert = (Button) findViewById(R.id.insert);

        del = (Button) findViewById(R.id.del);

        select = (Button) findViewById(R.id.select);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor query = contentResolver.query(uri, null, null, null, null);
                if(query != null){
                    while(query.moveToNext()){
                        String name = query.getString(query.getColumnIndex("name"));
                        Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        //删除
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentResolver.delete(uri,"id=?",new String[]{"1"});
            }
        });

        //插入
        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("name","大明");

                contentResolver.insert(uri,contentValues);
            }
        });
    }
}
